package org.dromara.web.service.business;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.Article;
import org.dromara.dao.business.domain.ArticleI18n;
import org.dromara.dao.business.domain.ArticleQuery;
import org.dromara.dao.business.mapper.ArticleI18nMapper;
import org.dromara.dao.business.mapper.ArticleMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class ArticleService {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    ArticleI18nMapper articleI18nMapper;
    @Autowired
    SettingsService settingsService;


    public TableDataInfo<Article> queryPageList(ArticleQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                articleMapper.paginationSelectList(query, pageQuery),
                articleMapper.paginationSelectCount(query)
        );
    }


    public Article queryById(Long id) {
        Article article = articleMapper.selectById(id);
        if (article != null) {
            List<ArticleI18n> i18nList = articleI18nMapper.selectList(new LambdaQueryWrapper<ArticleI18n>().eq(ArticleI18n::getArticleId, id));
            article.setI18ns(JSON.toJSONString(i18nList));
        }
        return article;
    }


    @Transactional
    public Article insert(Article entity) {
        articleMapper.insert(entity);
        if (StringUtils.isNotEmpty(entity.getI18ns())) {
            JSONArray jsonArray = JSON.parseArray(entity.getI18ns());
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    ArticleI18n articleI18n = new ArticleI18n();
                    articleI18n.setArticleId(entity.getId());
                    articleI18n.setLang(jsonObject.getString("lang"));
                    articleI18n.setContent(jsonObject.getString("content"));
                    articleI18n.setTitle(jsonObject.getString("title"));
                    articleI18nMapper.insert(articleI18n);
                }
            }
        }
        return entity;
    }

    @Transactional
    public void updateById(Article update) {

        Article entity = articleMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setTitle(update.getTitle());
        entity.setDescription(update.getDescription());
        entity.setUpdateTime(LocalDateTime.now());
        articleI18nMapper.delete(new LambdaQueryWrapper<ArticleI18n>().eq(ArticleI18n::getArticleId, update.getId()));
        if (StringUtils.isNotEmpty(update.getI18ns())) {
            JSONArray jsonArray = JSON.parseArray(update.getI18ns());
            if (jsonArray.size() > 0) {
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    ArticleI18n articleI18n = new ArticleI18n();
                    articleI18n.setArticleId(entity.getId());
                    articleI18n.setLang(jsonObject.getString("lang"));
                    articleI18n.setContent(jsonObject.getString("content"));
                    articleI18n.setTitle(jsonObject.getString("title"));
                    articleI18nMapper.insert(articleI18n);
                }
            }
        }

        articleMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        articleMapper.deleteById(id);
        articleI18nMapper.delete(new LambdaQueryWrapper<ArticleI18n>().eq(ArticleI18n::getArticleId, id));
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        articleMapper.deleteByIds(Arrays.asList(ids));
        articleI18nMapper.delete(new LambdaQueryWrapper<ArticleI18n>().in(ArticleI18n::getArticleId, Arrays.asList(ids)));
    }


    public Article getI18nArticle(Long id, String lang) {
        Article article = new Article();
        List<ArticleI18n> articleI18n = articleI18nMapper.selectList(new LambdaQueryWrapper<ArticleI18n>().eq(ArticleI18n::getArticleId, id).eq(ArticleI18n::getLang, lang));
        if (articleI18n != null && CollectionUtils.isNotEmpty(articleI18n)) {
            ArticleI18n i18n = articleI18n.get(0);
            article.setTitle(i18n.getTitle());
            article.setContent(i18n.getContent());
        }
        return article;
    }


    public Article getPrivacyArticle(String lang) {
        Map<String, String> settings = settingsService.getSettingsMap();
        Long id = Long.parseLong(settings.get("common_privacy_article_id"));
        return getI18nArticle(id, lang);
    }

    public Article getServiceArticle(String lang) {
        Map<String, String> settings = settingsService.getSettingsMap();
        Long id = Long.parseLong(settings.get("common_service_article_id"));
        return getI18nArticle(id, lang);
    }
}
